﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MOD;
using DAL;

namespace BLL
{
    public class B_Fence
    {
        private int iDataConn =  DataBase.IN_S1;
        public DataTable Select( int iUserId )
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();

            sCmdText.Append("Select * from Fence ");
            sCmdText.Append("where UserId = @UserId ");
            SqlParameter[] sSp = {
                                   new SqlParameter("@UserId",SqlDbType.Int)
                                 };
            sSp[0].Value = iUserId;

            return dDb.GetDataTable( sCmdText.ToString(), iDataConn, sSp );
        }
        public int Insert(M_Fence mFe)
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();
            
            sCmdText.Append("Insert into Fence ");
            sCmdText.Append("(UserId,name,sLat,sLon,bLat,bLon,type,cheid,cdatetime,ddatetime )");
            sCmdText.Append("values");
            sCmdText.Append("(@UserId,@name,@sLat,@sLon,@bLat,@bLon,@type,@cheid,@cdatetime,@ddatetime)");

            SqlParameter[] sSp = {
                                   new SqlParameter("@UserId",SqlDbType.Int),
                                   new SqlParameter("@name",SqlDbType.Char,20),
                                   new SqlParameter("@slat",SqlDbType.Float),
                                   new SqlParameter("@slon",SqlDbType.Float),
                                   new SqlParameter("@blat",SqlDbType.Float),
                                   new SqlParameter("@blon",SqlDbType.Float),
                                   new SqlParameter("@type",SqlDbType.Int),
                                   new SqlParameter("@cheid",SqlDbType.Text),
                                   new SqlParameter("@cdatetime",SqlDbType.DateTime), 
                                   new SqlParameter("@ddatetime",SqlDbType.DateTime)
                                };
            sSp[0].Value = mFe.Userid;
            sSp[1].Value = mFe.Name;
            sSp[2].Value = mFe.Slat;
            sSp[3].Value = mFe.Slon;
            sSp[4].Value = mFe.Blat;
            sSp[5].Value = mFe.Blon;
            sSp[6].Value = mFe.Type;
            sSp[7].Value = mFe.Cheid;
            sSp[8].Value = mFe.Cdatetime;
            sSp[9].Value = mFe.Ddatetime;

            return dDb.ExecuteCmd(sCmdText.ToString(), iDataConn, sSp);
        }
        public int Delete(int iId)
        {
            DataBase dDb = new DataBase();
            StringBuilder sCmdText = new StringBuilder();

            sCmdText.Append("Delete Fence ");
            sCmdText.Append("where Id = @Id ");
            SqlParameter[] sSp = {
                                   new SqlParameter("@Id",SqlDbType.Int)
                                 };
            sSp[0].Value = iId;

            return dDb.ExecuteCmd(sCmdText.ToString(), iDataConn, sSp);
        } 
    }
}
